﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MMPS.Business;
using MMPS.Entity.POCOMdel;
using MMPS.Common;
using MMPS.Entity.ViewModel;

namespace MMPS.Business
{
    public class CardLevelsService:BaseService<CardLevels>
    {
        #region  显示会员等级列表信息并分页显示
        public OperateResult GetCardLeves(CardLevelsGetViewModel viewModel)
        {
            //定义一个恒等式
            var query = PredicateExtensions.True<CardLevels>();
            //判断需要查询的会员等级名称是否为空
            if (!string.IsNullOrWhiteSpace(viewModel.LevelName))
            {
                query = query.And(s => s.CL_LevelName.Contains(viewModel.LevelName));
            }
            //定义一个变量来接收分页之后的总条数
            int rowCount = 0;
            //定义一个变量来接收查询出来的分页数据
            var pageCount = GetList(viewModel.page, viewModel.rows, ref rowCount, query, c => c.CL_ID, true).Select(c => new CardLevelsViewModel
            {
                CL_ID = c.CL_ID,
                CL_LevelName=c.CL_LevelName,
                CL_NeedPoint=c.CL_NeedPoint,
                CL_Percent=c.CL_Percent,
                CL_Point=c.CL_Point
            }).ToList();
            //将查询出来的总条数和每一页的总行数赋值给dataGrid
            var dataGrid = new CardLevelsDataGridViewModel {total=rowCount,rows=pageCount };
            //返回公共类型  给出提示信息 并显示数据
            return new OperateResult(ResultStatus.Success,"",dataGrid);
        }
        #endregion

        #region    添加会员等级信息
        public OperateResult GetCardLevesInsert(CardLevelsViewModel viewModel)
        {
            //将视图模型转成EF模型
            var model = new CardLevels()
            {
                CL_LevelName=viewModel.CL_LevelName,
                CL_NeedPoint=viewModel.CL_NeedPoint,
                CL_Percent=viewModel.CL_Percent,
                CL_Point=viewModel.CL_Point,
            };
            //把转好的EF模型数据添加到列表中  判断给出提示信息
            if (Add(model))
            {
                return new OperateResult(ResultStatus.Success, "添加成功！");
            }
            else
            {
                return new OperateResult(ResultStatus.Error,"添加失败");
            }
        }
        #endregion

        #region    修改会员等级信息
        /// <summary>
        /// 修改会员等级信息之前先要通过选中行的ID查询出来要修改的信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public OperateResult CardLevesEditById(int Id)
        {
            //先找到选中行的Id
            var model = Find(c=>c.CL_ID==Id);
            //判断要修改的会员ID是否存在 若存在则显示，若不存在则弹窗提示
            if (model == null)
            {
                return new OperateResult(ResultStatus.Success, "该用户不存在！");
            }
            else
            {
                var viewModel = new CardLevelsViewModel()
                {
                    CL_ID = model.CL_ID,
                    CL_LevelName = model.CL_LevelName,
                    CL_NeedPoint = model.CL_NeedPoint,
                    CL_Percent = model.CL_Percent,
                    CL_Point = model.CL_Point,
                };
                return new OperateResult(ResultStatus.Success,"",viewModel);
            }
        }
        public OperateResult EditCardLevesList(CardLevelsViewModel viewModel)
        {
            var model = Find(c=>c.CL_ID==viewModel.CL_ID);
            //判断model里是否有数据  若没有数据则提示“该会员不存在”
            if (model!=null)
            {
                //将视图模型转换成EF模型数据  把你更改后显示的数据放到model里
                model.CL_LevelName = viewModel.CL_LevelName;
                model.CL_NeedPoint = viewModel.CL_NeedPoint;
                model.CL_Percent = viewModel.CL_Percent;
                model.CL_Point = viewModel.CL_Point;
                //判断是否更新
                if (Update(model))
                {
                    return new OperateResult(ResultStatus.Success,"修改会员等级信息成功！");
                }
                else
                {
                    return new OperateResult(ResultStatus.Error, "修改会员等级信息失败！");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Error,"该会员不存在！");
            }
        }
        #endregion

        #region    删除会员等级信息
        /// <summary>
        /// 通过会员等级的指定ID进行删除整行数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult DeleteCardLevesRows(int Id)
        {
            //先找到选中行的Id
            var model = Find(c => c.CL_ID == Id);
            //判断选中行的Id是否存在 ，存在则删除，不存在则弹出信息提示
            if (model==null)
            {
                return new OperateResult(ResultStatus.Success, "该用户不存在！");
            }
            //删除
            if (Delete(model))
            {
                return new OperateResult(ResultStatus.Success, "删除成功！");
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "删除失败！");
            }
        }
        #endregion
    }
}
